package com.api.util.repo.impl;

import com.api.test.Api;
import com.api.util.repo.DtoFactory;
import com.api.util.repo.DtoRepository;
import com.zyncro.api.IDepartmentApiService;
import com.zyncro.api.model.ObjectResultApi;

public class DepartmentDtoFactory implements DtoFactory<String> {

	private final Api api;
	private final DomainUser mainAdmin;

	public DepartmentDtoFactory(Api api, DomainUser mainAdmin) {
		this.api = api;
		this.mainAdmin = mainAdmin;
	}

	@Override
	public String createDto(String property, DtoRepository dtoRepository) {

		try {

			api.login(mainAdmin.getAppId(), mainAdmin.getPassword());

			String name = property;
			String description = null;
			String address = null;
			String telephone = null;
			String email = null;
			String web = null;
			String fax = null;
			boolean showMembers = true;
			String idUserOwner = null;
			String attributes = null;
			boolean followerComment = true;

			ObjectResultApi<String> result = api.getService(IDepartmentApiService.class).createDepartment(name, description, address,
					telephone, email, web, fax, showMembers, idUserOwner, attributes, followerComment);

			return result.getObject();

		} catch (Exception e) {
			throw new IllegalStateException(String.format("Couldn't create department for property '%s'", property), e);
		}
	}
}
